<template>
  <el-form label-width="120px" :model="data" ref="formRef" :rules="rule">
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="data.email" placeholder="请输入邮箱"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="data.password"
        type="password"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">登录</el-button>
    </el-form-item>
  </el-form>
</template>
<script setup>
import { login } from "@/api/users";
import { useUserStore } from "@/stores/userStore";
import { ElMessage } from "element-plus";
import { ref } from "vue";
const formRef = ref(null);
const emits = defineEmits(["close"]);
const userStore = useUserStore();
const data = ref({
  email: "",
  password: "",
});
const submit = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let info = await login(data.value);
      userStore.userInfo = info.data.data.userProfiles;
      userStore.token = info.data.data.token;
      userStore.isLogin = true;
      ElMessage.success("登录成功");
      emits("close");
    } else {
      console.log("error submit!!");
      return false;
    }
  });
};
</script>
